package jmine.tec.utils.collection;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;

/**
 * @author jrenaut
 */
public class FoldUtilsShould {

    @Test
    public void leftFoldAnEmptyCollection() {
        Object start = new Object();
        Object result = FoldUtils.leftFold(Collections.emptyList(), start, NO_OP);
        Assert.assertSame(start, result);
    }

    @Test
    public void leftFoldACollection() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        Integer sum = FoldUtils.leftFold(list, 0, SUM_INT);
        Assert.assertEquals(15, sum.intValue());
    }

    private static SymetricFoldOperation<Integer> SUM_INT = new SymetricFoldOperation<Integer>() {
        public Integer apply(Integer s, Integer t) {
            return s + t;
        }
    };

    private static SymetricFoldOperation<Object> NO_OP = new SymetricFoldOperation<Object>() {
        public Object apply(Object s, Object t) {
            return null;
        }
    };
}
